#include <regx52.h>

sbit N_R = P2 ^ 0;
sbit N_G = P2 ^ 1;
sbit N_Y = P2 ^ 2;
sbit E_R = P2 ^ 3;
sbit E_G = P2 ^ 4;
sbit E_Y = P2 ^ 5;
sbit K1 = P3 ^ 2;  // 紧急按钮
sbit K2 = P3 ^ 3;  // 夜间模式
sbit K3 = P3 ^ 4;  // 行人按钮

#define NORMAL 0
#define EMERGENCY 1
#define NIGHT 2

unsigned char system_state = NORMAL;
unsigned char normal_phase = 0;    // 0:阶段1, 1:阶段2, 2:阶段3, 3:阶段4
unsigned char green_extended = 0;   // 绿灯延长标志
unsigned int phase_timer = 0;       // 阶段计时器
unsigned char blink_state = 0;      // 闪烁状态

void delay_ms(unsigned int ms) {
    unsigned int i, j;
    for (i = 0; i < ms; i++) {
        for (j = 0; j < 123; j++);
    }
}

void key_scan() {
    static unsigned char key1_pressed = 0, key2_pressed = 0, key3_pressed = 0;
    
    // 检测K1（紧急模式）
    if (K1 == 0) {
        if (!key1_pressed) {
            delay_ms(10); // 消抖
            if (K1 == 0) {
                if (system_state == EMERGENCY) {
                    system_state = NORMAL; // 退出紧急模式
                    normal_phase = 0;     // 重置交通灯相位
                } else {
                    system_state = EMERGENCY; // 进入紧急模式
                }
                key1_pressed = 1;
            }
        }
    } else {
        key1_pressed = 0;
    }
    
    // 检测K2（夜间模式）
    if (K2 == 0) {
        if (!key2_pressed) {
            delay_ms(10);
            if (K2 == 0) {
                if (system_state == NIGHT) {
                    system_state = NORMAL; // 退出夜间模式
                    normal_phase = 0;
                } else {
                    system_state = NIGHT; // 进入夜间模式
                }
                key2_pressed = 1;
            }
        }
    } else {
        key2_pressed = 0;
    }
    
    // 检测K3（行人模式）
    if (K3 == 0) {
        if (!key3_pressed) {
            delay_ms(10);
            if (K3 == 0) {
                // 仅在正常模式下的绿灯阶段且未延长时生效
                if (system_state == NORMAL && 
                   (normal_phase == 0 || normal_phase == 2) && 
                   !green_extended) {
                    green_extended = 1; // 标记延长
                }
                key3_pressed = 1;
            }
        }
    } else {
        key3_pressed = 0;
    }
}

void main(void) {
    unsigned char i;
    P2 = 0xFF; // 初始所有灯灭
    
    while (1) {
        key_scan(); // 扫描按键
        
        switch (system_state) {
            case NORMAL:
                switch (normal_phase) {
                    case 0: // 阶段1：南北绿灯，东西红灯
                        N_R = 1; N_G = 0; N_Y = 1;
                        E_R = 0; E_G = 1; E_Y = 1;
                        phase_timer++;
                        
                        // 基础5秒 + 延长5秒（如果K3按下）
                        if (phase_timer >= (green_extended ? 1000 : 500)) {
                            phase_timer = 0;
                            normal_phase = 1; // 进入下一阶段
                            green_extended = 0; // 重置延长标志
                        }
                        delay_ms(5);
                        break;
                        
                    case 1: // 阶段2：南北黄灯闪烁
                        N_G = 1; // 关闭绿灯
                        for (i = 0; i < 3; i++) {
                            N_Y = 0; // 黄灯亮
                            delay_ms(500);
                            N_Y = 1; // 黄灯灭
                            delay_ms(500);
                        }
                        normal_phase = 2; // 进入下一阶段
                        break;
                        
                    case 2: // 阶段3：东西绿灯，南北红灯
                        E_R = 1; E_G = 0; E_Y = 1;
                        N_R = 0; N_G = 1; N_Y = 1;
                        phase_timer++;
                        
                        if (phase_timer >= (green_extended ? 1000 : 500)) {
                            phase_timer = 0;
                            normal_phase = 3;
                            green_extended = 0;
                        }
                        delay_ms(5);
                        break;
                        
                    case 3: // 阶段4：东西黄灯闪烁
                        E_G = 1; // 关闭绿灯
                        for (i = 0; i < 3; i++) {
                            E_Y = 0;
                            delay_ms(500);
                            E_Y = 1;
                            delay_ms(500);
                        }
                        normal_phase = 0; // 回到阶段1
                        break;
                }
                break;
                
            case EMERGENCY: // 紧急模式：全方向红灯闪烁
                blink_state = !blink_state;
                N_R = blink_state ? 0 : 1;
                E_R = blink_state ? 0 : 1;
                N_G = 1; N_Y = 1; E_G = 1; E_Y = 1;
                delay_ms(500); // 500ms闪烁周期
                break;
                
            case NIGHT: // 夜间模式：全方向黄灯闪烁
                blink_state = !blink_state;
                N_Y = blink_state ? 0 : 1;
                E_Y = blink_state ? 0 : 1;
                N_R = 1; N_G = 1; E_R = 1; E_G = 1;
                delay_ms(500);
                break;
        }
    }
}